package com.papercutting.platform.controller;

import com.papercutting.platform.common.Result;
import com.papercutting.platform.dto.request.AIChatRequest;
import com.papercutting.platform.dto.response.AIChatResponse;
import com.papercutting.platform.service.AIService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

/**
 * AI助手控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/ai")
public class AIController {
    
    @Autowired
    private AIService aiService;
    
    /**
     * AI聊天接口（普通模式）
     */
    @PostMapping("/chat")
    public Result<AIChatResponse> chat(@Validated @RequestBody AIChatRequest request) {
        log.info("收到AI聊天请求，消息: {}", request.getMessage());
        
        try {
            AIChatResponse response = aiService.chat(request);
            return Result.success("对话成功", response);
        } catch (Exception e) {
            log.error("AI聊天失败", e);
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * AI流式聊天接口
     */
    @PostMapping("/stream-chat")
    public SseEmitter streamChat(@Validated @RequestBody AIChatRequest request) {
        log.info("收到流式AI聊天请求，消息: {}", request.getMessage());
        return aiService.streamChat(request);
    }
    
    /**
     * 清除对话历史（可选功能）
     */
    @PostMapping("/clear-history")
    public Result<Void> clearHistory() {
        // 这是一个占位接口，由于我们是无状态的，历史记录由前端维护
        // 如果需要服务端管理会话历史，可以使用Redis等存储
        return Result.success("对话历史已清除");
    }
}

